# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import redis


class AnalysisPipeline(object):
    def __init__(self):
        self.res_dict = {}
        self.res_set = set()
        self.db = redis.Redis("192.168.200.203")

    def process_item(self, item, spider):
        if self.res_dict.get(item["week"]):
            self.res_dict.get(item["week"]).append(item["classmate_name"])
        else:
            self.res_dict[item["week"]] = [item["classmate_name"]]

        self.res_set.add(item["classmate_name"])

        return item

    def close_spider(self, spider):
        print("每周交了作业的同学")
        for key in self.res_dict:
            print(key, self.res_dict[key])

        print("="*30)
        print("本期提交作业总人数")
        print("总共有 {} 人提交".format(len(self.res_set)))

        print("="*30)
        print("每周提交作业人数")
        for key in self.res_dict:
            length = len(self.res_dict[key])
            self.db.hset("data", key, length)
            print("{} 有 {} 人提交作业".format(key, length))